package com.css.fxfzaqbz.modules.assessplot.controller;

import com.css.fxfzaqbz.base.response.RestResponse;
import com.css.fxfzaqbz.common.utils.excel.ExportUtil;
import com.css.fxfzaqbz.modules.assess.entity.CasualtiesEntity;
import com.css.fxfzaqbz.modules.assess1.entity.AlgEntity;
import com.css.fxfzaqbz.modules.assessplot.entity.EvaluationResults;
import com.css.fxfzaqbz.modules.assessplot.service.EvaluationResultsService;
import com.css.fxfzaqbz.modules.base.entity.HouseDestoryEntity;
import com.css.fxfzaqbz.modules.base.entity.SeismicResult;
import com.css.fxfzaqbz.modules.base.service.SeismicResultService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

//评估结果
@RestController
@RequestMapping("/assessplot/evaluationResults")
@Slf4j
public class EvaluationResultsController {
    @Resource
    private EvaluationResultsService evaluationResultsService;
    @Resource
    private SeismicResultService seismicResultService;

    //保存评估结果
    @PostMapping("/save")
    public RestResponse save(@RequestBody EvaluationResults evaluationResults) {
        RestResponse restResponse = null;
        try {
            evaluationResultsService.save(evaluationResults);
            restResponse = RestResponse.succeed(evaluationResults);
        } catch (Exception e) {
            log.error("保存失败", e);
            restResponse = RestResponse.fail("保存失败!");
        }
        return restResponse;
    }

    //修改展示状态  传参 1修改哪条的id  2该数据的result  3要修改成的状态
    @PostMapping("/updateStatus")
    public RestResponse updateStatus(@RequestBody EvaluationResults evaluationResults) {
        RestResponse restResponse = null;
        try {
            evaluationResultsService.updateStatus(evaluationResults);
            restResponse = RestResponse.succeed(evaluationResults);
        } catch (Exception e) {
            log.error("修改状态失败", e);
            restResponse = RestResponse.fail("修改状态失败!");
        }
        return restResponse;
    }
    //查询评估结果
    @GetMapping("/getAll")
    public RestResponse getAll(EvaluationResults evaluationResults) {
        RestResponse restResponse = null;
        try {
            List<EvaluationResults> list=evaluationResultsService.getAll(evaluationResults);
            restResponse = RestResponse.succeed(list);
        } catch (Exception e) {
            log.error("查看失败", e);
            restResponse = RestResponse.fail("查看失败!");
        }
        return restResponse;
    }

    //下载抗震能力结果
    @PostMapping("/exportExcel/{id}")
    public void exportExcel(@PathVariable String id, HttpServletResponse response){
        try {
            List<SeismicResult> list = seismicResultService.getById(id);
            ExportUtil.createExcel(response,list,"抗震能力结果数据",SeismicResult.class);
        } catch (Exception e) {
            log.error("抗震能力结果数据导出失败",e);
        }
    }

    //下载人口伤亡和需紧急安置人员数据
    @PostMapping("/export/{id}")
    public void export(@PathVariable String id, HttpServletResponse response){
        try {
            List<AlgEntity> list = seismicResultService.getData(id);
            ExportUtil.createExcel(response,list,"人口伤亡及需紧急安置人员结果数据",AlgEntity.class);
        } catch (Exception e) {
            log.error("人口伤亡和需紧急安置人员结果数据导出失败",e);
        }
    }

    //根据活动id跟房屋破坏类型result获取默认评估结果 有评估结果返回结果id,没有返回null
    @GetMapping("/getResult")
    public RestResponse getResult(@RequestParam String iaId){
        try {
            String id = evaluationResultsService.getResult(iaId);
            return RestResponse.succeed(id);
        } catch (Exception e) {
            log.error("获取失败",e);
            return RestResponse.fail("获取失败");
        }
    }
}
